package site.xuxingcan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * Created by xuxingcan on 2016/8/6.
 */
public class MysqlUtil {
    private static volatile MysqlUtil instance;
    private static String MYSQL_URL = "jdbc:mysql://" + Main.MYSQL_URI + "?user=" + Main.MYSQL_USER + "&password=" + Main.MYSQL_PWD + "&useUnicode=true&characterEncoding=UTF8";
    private Connection conn;
    private ArrayList<String> imeis = new ArrayList<>();

    public static MysqlUtil getInstance() {
        if (instance == null) {
            synchronized (MysqlUtil.class) {
                if (instance == null) {
                    instance = new MysqlUtil();
                }
            }
        }
        return instance;
    }

    private MysqlUtil() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(MYSQL_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isImeiRight(String imei) {
        if (imeis.contains(imei)) {
            return true;
        }
        try {
            String sql = "select imei from user;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                if (!imeis.contains(rs.getString("imei"))) {
                    imeis.add(rs.getString("imei"));
                }
            }
            if (imeis.contains(imei)) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void insertOperation(String imei, String file_name) {
        try {
            String sql = " INSERT INTO operation(imei,file_name) VALUES('" + imei + "','" + file_name + "');";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertImei(String imei) {
        try {
            String sql = " INSERT INTO user(imei) VALUES('" + imei + "');";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertErrorImei(String imei) {
        try {
            String sql = " INSERT INTO error_imei(imei,times) VALUES('" + imei + "','1');";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateErrorImei(String imei, int times) {
        try {
            String sql = "update error_imei set times = " + times + " where imei like '" + imei + "';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getErrorImeiTimes(String imei) {
        try {
            String sql = "select times from error_imei where imei like '" + imei + "';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                return rs.getInt("times");
            }
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

//    private static String string2MD5LowerCase(String inStr) {
//        MessageDigest md5 = null;
//        try {
//            md5 = MessageDigest.getInstance("MD5");
//        } catch (Exception e) {
//            System.out.println(e.toString());
//            e.printStackTrace();
//            return "";
//        }
//        char[] charArray = inStr.toCharArray();
//        byte[] byteArray = new byte[charArray.length];
//
//        for (int i = 0; i < charArray.length; i++)
//            byteArray[i] = (byte) charArray[i];
//        byte[] md5Bytes = md5.digest(byteArray);
//        StringBuffer hexValue = new StringBuffer();
//        for (int i = 0; i < md5Bytes.length; i++) {
//            int val = ((int) md5Bytes[i]) & 0xff;
//            if (val < 16)
//                hexValue.append("0");
//            hexValue.append(Integer.toHexString(val));
//        }
//        return hexValue.toString().toLowerCase();
//    }

    public void heartBeat() {
        try {
            String sql = "select imei from user where imei = 'heartBeat';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            System.out.println("heartBeat");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
